package com.tuoyu.peopletest.view.fragment;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.NetworkUtils;
import com.lxj.xpopup.XPopup;
import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.common.MyApplication;
import com.tuoyu.peopletest.entity.message.ForegroundMessageEvent;
import com.tuoyu.peopletest.entity.message.MyMessageEvent;
import com.tuoyu.peopletest.entity.message.NetWorkMessageEvent;
import com.tuoyu.peopletest.presenter.LoadUIPresenter;
import com.tuoyu.peopletest.utils.SCLogUtils;
import com.tuoyu.peopletest.view.ac.BaseActivity;
import com.tuoyu.peopletest.view.iview.IView;
import com.tuoyu.peopletest.view.pop.ResultPop;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * fragment 基础类
 */
public abstract class BaseFragment extends ImmersionFragment implements IView {

    public final String TAG = this.getClass().getSimpleName();
    public BaseActivity mBaseAc;

    /**
     * onCreateView()里返回的view，修饰为protected,所以子类继承该类时，在onCreateView里必须对该变量进行初始化
     */
    protected ViewGroup rootView;
    //基础总布局
    public FrameLayout vBaseMain;

    //toast布局
    private TextView vBaseToast;
    private View vLoadMain;

    //加载框显示动画
    private ObjectAnimator objAnimShowProgress;

    public boolean isConnectNet;

    //页面是否可见
    public boolean isResume;

    //是否显示过
    private boolean isVisibled = false;

    public LoadUIPresenter mLoadUIPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SCLogUtils.e(TAG, "onCreate");
        EventBus.getDefault().register(this);
        isConnectNet = MyApplication.isConnectNet;
        mBaseAc = (BaseActivity) getActivity();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mBaseAc = (BaseActivity) getActivity();
        SCLogUtils.e(TAG, "onActivityCreated");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        SCLogUtils.e(TAG, "onCreateView");
        if (rootView == null) {
            rootView = (ViewGroup) inflater.inflate(R.layout.fragment_base, null);
            vBaseMain = rootView.findViewById(R.id.v_f_base_main);

            FrameLayout vBaseContent = rootView.findViewById(R.id.sc_base_content);
            vBaseToast = rootView.findViewById(R.id.sc_base_toast);
            vBaseToast.setAlpha(0);

            //vLoadMain = rootView.findViewById(R.id.v_load_main);
            //vLoadMain.setVisibility(View.INVISIBLE);
            //添加页面布局
            View mainView = View.inflate(mBaseAc, getViewRes(), null);
            vBaseContent.addView(mainView);

            //加载弹窗
            mLoadUIPresenter = LoadUIPresenter.get();
            mLoadUIPresenter.setMainView(vBaseMain);
            setLoadingUI();

            //初始化子view
            initChildView(rootView);
            //设置时间监听
            initListener();
        }
        return rootView;
    }

    /**
     * 多次调用
     *
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SCLogUtils.e(TAG, "onViewCreated");

    }

    /**
     * 沉浸式代码写着这里
     */
    @Override
    public void initImmersionBar() {

    }

    /**
     * 获取view 资源id
     *
     * @return
     */
    public abstract int getViewRes();

    /**
     * 加载子view
     *
     * @return
     */
    public abstract void initChildView(View rootView);

    public void initListener() {

    }

    public void initListenerOnce() {

    }

    /**
     * 每次显示都调用
     * 有些view,在变化, 要多次注册点击监听
     */
    public void initVisibleListener() {

    }

    @Override
    public void onStart() {
        super.onStart();
        SCLogUtils.e(TAG, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        SCLogUtils.e(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        SCLogUtils.e(TAG, "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        SCLogUtils.e(TAG, "onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        SCLogUtils.e(TAG, "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SCLogUtils.e(TAG, "onDestroy");
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        SCLogUtils.e(TAG, "onDetach");
    }

    /**
     * 在view初始化之前调用
     */
    @Override
    public void onLazyBeforeView() {
        super.onLazyBeforeView();
    }

    /**
     * 在view初始化之后调用
     */
    @Override
    public void onLazyAfterView() {
        super.onLazyAfterView();
    }

    /**
     * 当前Fragment对用户可见的时候调用
     */
    @Override
    public void onVisible() {
        super.onVisible();
        SCLogUtils.e(TAG, "页面可见 onVisible");
        isResume = true;

        //数据初始化一次
        if (!isVisibled) {
            initDataOnce();
            initListenerOnce();
            isVisibled = true;
        }

        initVisibleListener();
    }

    /**
     * 当前Fragment不可见的时候调用
     */
    @Override
    public void onInvisible() {
        super.onInvisible();
        SCLogUtils.e(TAG, "onInvisible");
        isResume = false;
    }

    /**
     * 第一次显示
     */
    public void initDataOnce() {
        SCLogUtils.e(TAG, "数据 initDataOnce");
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onForegroundEvent(ForegroundMessageEvent event) {
        //前后台切换了
        if (event.isAppForeground) {
            onForeground(event.ac);
        } else {
            onBackground(event.ac);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNetWorkEvent(NetWorkMessageEvent event) {
        //网络变化
        if (event.isNetConnected) {
            //连接
            onConnected(event.networkType);
        } else {
            //断开
            onDisconnected();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNotifyEvent(MyMessageEvent event) {
        if (event.isBackAc) {
            //依附activity点击返回
            if (getActivity().getClass().getSimpleName().equals(event.mAcTag)) {
                //依附的activity 点击了返回
                SCLogUtils.e(TAG, "点击返回 " + event.mAcTag);
                onBackCloseLoading();
            }
        } else {
            onNotice(event);
        }

    }

    /**
     * 是否在请求网络数据
     *
     * @return
     */
    public boolean isRquestNetData() {
        return false;
    }


    /**
     * 通过返回键关闭加载框
     */
    public void onBackCloseLoading() {

    }

    /**
     * 获得EventBus通知
     *
     * @param event
     */
    protected void onNotice(MyMessageEvent event) {

    }

    /**
     * 设置价值UI
     */
    public void setLoadingUI() {
        mLoadUIPresenter.setType(LoadUIPresenter.NOMAL);
        mLoadUIPresenter.initNomalUI();
    }

    @Override
    public void showLoading() {
        mLoadUIPresenter.showLoading();
    }

    @Override
    public void hideLoading() {
        mLoadUIPresenter.hideLoading();
    }

    @Override
    public void showToast(Object obj) {

        if (obj instanceof Integer) {
            vBaseToast.setText((Integer) obj);
        } else if (obj instanceof String) {
            vBaseToast.setText((String) obj);
        }


        ObjectAnimator objAnimToast = ObjectAnimator.ofFloat(vBaseToast, "alpha", 0f, 0.8f, 1f, 0.5f, 0f);
        objAnimToast.setDuration(3000);
        objAnimToast.start();
    }

    @Override
    public void showToastTop(Object obj) {


    }

    @Override
    public void showDialogResult(Object obj) {
        ResultPop pop = new ResultPop(mBaseAc,obj);
        new XPopup.Builder(mBaseAc)
                .dismissOnTouchOutside(true)
                .asCustom(pop)
                .show();
    }

    /**
     * 网络变化监听器
     */
    public void onDisconnected() {
        SCLogUtils.e(TAG, "网络变化不可用");
        //网络是否可用,
        isConnectNet = false;
    }

    /**
     * 网络重新连接回调
     *
     * @param networkType
     */
    public void onConnected(NetworkUtils.NetworkType networkType) {
        isConnectNet = true;
        SCLogUtils.e(TAG, "网络变化 可用");
    }

    /**
     * app 回到前台回调
     *
     * @param ac 最顶部的ac
     */
    public void onForeground(Activity ac) {

    }

    /**
     * app推入后台回调
     *
     * @param ac 最顶部的ac
     */
    public void onBackground(Activity ac) {

    }

    /**
     * 是否可用返回
     * @return
     */
    public boolean isOnBackPressed() {
        return true;
    }

}
